package com.tengju.bff.interfaces.covercharge.controller.notify;

import com.tengju.bff.interfaces.covercharge.constant.NotifyBizImplEnum;
import com.zoi7.component.core.exception.ServiceException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author shiliang
 * @date 2020.06.12 15:48
 * @description 业务实现类工厂
 */
@Data
@Slf4j
@Service
public class NotifyBizFactory implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    /**
     * 获取业务实现类
     *
     * @param channel 渠道
     * @param bizType 业务类型
     * @param <T>     返回实现类
     * @return 结果
     */
    public <T> T getBizFactoryImpl(String channel, String bizType) {
        NotifyBizImplEnum bizImplEnum = NotifyBizImplEnum.getInstance(channel, bizType);
        if (Objects.isNull(bizImplEnum)) {
            throw new ServiceException("未找到业务实现类");
        }
        Class<T> clazz = bizImplEnum.getClazz();
        return applicationContext.getBean(clazz);

    }

}
